package com.demo.service.impl;

import com.demo.mapper.ComployeeMapper;
import com.demo.pojo.Comployee;
import com.demo.pojo.Comployee;
import com.demo.pojo.Dept;
import com.demo.service.ComployeeService;
import com.demo.utils.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author： Administrator
 * @date: 21/05/09 16:29
 * 描述：
 */
@Service
public class ComployeeServiceImpl implements ComployeeService {
    @Autowired
    private ComployeeMapper comployeeMapper;

    @Override
    public int add(Comployee comployee) {
        int add = 0;
        comployee.setComployeeStatu("在职");
        for (int i = 0; i < 10; i++) {
            comployee.setComployeeNo("C" + System.currentTimeMillis());
            add = comployeeMapper.insertSelective(comployee);
            if (add > 0) {
                return add;
            }
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        return add;
    }

    @Override
    public int del(String no) {
        return comployeeMapper.deleteByPrimaryKey(no);
    }

    @Override
    public int update(Comployee comployee) {
        return comployeeMapper.updateByPrimaryKeySelective(comployee);
    }

    @Override
    public Page<Comployee> page(Comployee comployee, int pageNo, int pageSize) {
        Page<Comployee> page = new Page<>();
        page.setPageSize(pageSize);
        int count = comployeeMapper.pageCount(comployee);
        page.setPageTotalCount(count);
        int total = count / pageSize;
        if (count % pageSize > 0) {
            total++;
        }
        page.setPageTotal(total);
        page.setPageNo(pageNo);
        List<Comployee> comployees = comployeeMapper.pageByCond(comployee, (pageNo - 1) * pageSize, pageSize);
        page.setItems(comployees);
        return page;
    }

    @Override
    public Comployee select(String comployeeNo) {
        return comployeeMapper.selectByPrimaryKey(comployeeNo);
    }
}
